/***
*
* File: wp_timers.nut
* Auth: Damian Wojdat
* Date: 20.10.2012
* Desc: Timered functions.
*
*************************************************************************************************************/

function halfSecTimer()
{
	local velo;
	local distance;
	local vehicleid;
	
	for(local i = 0; i < MAX_PLAYERS; i++)
	{
		if(isPlayerConnected(i) && pData[i].logged)
		{
			/*
			/ Player status
			*/
			if(pData[i].faction1 != -1 && pData[i].faction2 != -1)	
			{
				triggerClientEvent(i, "playerStatus", 1, 
					fData[pData[i].faction1].name, 
					factionRankName(pData[i].faction1, pData[i].faction1Rank), 
					fData[pData[i].faction2].name, 
					factionRankName(pData[i].faction2, pData[i].faction2Rank), 
					pData[i].playingHours);				
			}			
			else if(pData[i].faction1 == -1 && pData[i].faction2 == -1)
				triggerClientEvent(i, "playerStatus", 1, "brak", "n/a", "brak", "n/a", pData[i].playingHours);
			else if(pData[i].faction1 != -1 && pData[i].faction2 == -1)
			{
				triggerClientEvent(i, "playerStatus", 1,
					fData[pData[i].faction1].name,
					factionRankName(pData[i].faction1, pData[i].faction1Rank),
					"brak", "n/a", pData[i].playingHours);
			}
			else if(pData[i].faction1 == -1 && pData[i].faction2 != -1)
			{
				triggerClientEvent(i, "playerStatus", 1, "brak", "n/a",
					fData[pData[i].faction2].name,
					factionRankName(pData[i].faction2, pData[i].faction2Rank),
					pData[i].playingHours);
			}
				
			/*
			/ Vehicle status
			*/
			vehicleid = getPlayerVehicleId(i);
			if(vehicleid != false)
				triggerClientEvent(i, "playerCarStatus", 1, vehicleid, vehData[vehicleid].engineOn, vehData[vehicleid].fuel, vehData[vehicleid].counter);
			else
				triggerClientEvent(i, "playerCarStatus", 0, 0, 0, 0, 0);
			
			/*
			* On round time left score board force toggle - player part
			*/
			if(gRoundStartTime > 0 && !gScoreBoardForceToggled)
			{
				triggerClientEvent(i, "scoreBoardForceToggle", 1);
			}
			else if(gRoundStartTime == 0 && gScoreBoardForceToggled)
			{
				triggerClientEvent(i, "scoreBoardForceToggle", 0);
			}
		}
	}
	
	/*
	* On round time left score board force toggle - mechanism part
	*/
	if(gRoundStartTime > 0 && !gScoreBoardForceToggled)
	{
		gScoreBoardForceToggled = 1;
	}
	else if(gRoundStartTime == 0 && gScoreBoardForceToggled)
	{
		gScoreBoardForceToggled = 0;
	}
	
	
	return 1;
}

function oneSecTimer()
{
	local vehicleid;
	local velo;
	local distance;
	
	if(gRoundTimeLeft)
	{
		if(gRoundStartTime > 0)
			gRoundStartTime--;
		else
		{
			if(gRoundStartTime == 0)
			{
				gRoundStartTime--;
				// Reset faction kill stats
				for(local i = 0; i < factionsCount; i++)
				{
					fData[i].kills = 0;
				}
			}
			gRoundTimeLeft--;
			if(gRoundTimeLeft == 0)
				selectRandomRound();
		}
	}
	
	/*
	* Cooldown reducer
	*/							
	for(local i = 0; i < MAX_PLAYERS; i++)
	{
		if(isPlayerConnected(i) && pData[i].logged)
		{
			/*
			* Cooldown reducer
			*/
			if(gJustDropped[i] > 0)
				gJustDropped[i]--;		
			if(pData[i].timeToRespawn > 0)
			{
				pData[i].timeToRespawn--;
				displayPlayerText(i, 0.8, 0.8, "Respawn in " + pData[i].timeToRespawn, 1000);
				if(pData[i].timeToRespawn == 0)
					setPlayerSpawn(i);
			}			
			
			/*
			* Update client round time end
			*/
			triggerClientEvent(i, "roundTimeChange", gRoundTimeLeft);
	
			/*
			* Measure vehicle travel distance
			*/
			vehicleid = getPlayerVehicleId(i);
			if(vehicleid != false)
			{
				velo = getVehicleVelocity(vehicleid);
				distance = sqrt(pow(velo[0], 2) + pow(velo[1], 2) + pow(velo[2], 2));
				vehData[vehicleid].counter += distance;
			}
		}
	}
	return 1;
}

function oneMinTimer()
{
	foreach(i, playername in getPlayers())
	{
		if(pData[i].logged.tointeger() == 1)
		{
			/*
			* Player cooldowns
			*/
			if(pData[i].warnCooldown > 0)
			{
				pData[i].warnCooldown--;
				savePlayerData(i, "warnCooldown");
			}
			if(pData[i].muted > 0)
			{
				pData[i].muted--;
				savePlayerData(i, "muted");
			}
			
			/*
			* Player time play increase
			*/
			pData[i].playingHoursHelper++;
			if(pData[i].playingHoursHelper == 60)
			{
				pData[i].playingHoursHelper = 0;
				pData[i].playingHours++;
				savePlayerData(i , "playingHours");
			}
			savePlayerData(i , "playingHoursHelper");
		}
	}
	for(local i = 0; i < vehiclesCount; i++)
	{
		if(isVehicleValid(i))
		{
			vehData[i].dirtLvl = getVehicleDirtLevel(i);
			saveVehData(i.tointeger(), "dirtLvl");
		}
	}
}

function tenMinTimer()
{
	setWeather(random(1,9));
	return 1;
}